home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpuzzles.3 / xpuzzles / xpuzzles-5.3.1 / xskewb / SkewbU.c < prev    next >
C/C++ Source or Header  |  1996-02-05  |  7KB  |  263 lines

  1. /*
  2. # X-BASED SKEWB
  3. #
  4. #  SkewbU.c
  5. #
  6. ###
  7. #
  8. #  Copyright (c) 1994 - 96    David Albert Bagley, bagleyd@hertz.njit.edu
  9. #
  10. #                   All Rights Reserved
  11. #
  12. #  Permission to use, copy, modify, and distribute this software and
  13. #  its documentation for any purpose and without fee is hereby granted,
  14. #  provided that the above copyright notice appear in all copies and
  15. #  that both that copyright notice and this permission notice appear in
  16. #  supporting documentation, and that the name of the author not be
  17. #  used in advertising or publicity pertaining to distribution of the
  18. #  software without specific, written prior permission.
  19. #
  20. #  This program is distributed in the hope that it will be "playable",
  21. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. #
  24. */
  25.  
  26. /* Undo algorithm */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <X11/IntrinsicP.h>
  31. #include <X11/Intrinsic.h>
  32. #include <X11/StringDefs.h>
  33. #include <X11/CoreP.h>
  34. #include "SkewbP.h"
  35. #include "Skewb2dP.h"
  36. #include "Skewb3dP.h"
  37.  
  38. typedef struct _MoveRecord
  39. {
  40.   /* int face, position, direction, control; */
  41.   unsigned short int packed; /* This makes assumptions on the data. */
  42. } MoveRecord;
  43.  
  44. typedef struct _MoveStack
  45. {
  46.   MoveRecord move;
  47.   struct _MoveStack *previous, *next;
  48. } MoveStack;
  49.  
  50. static MoveStack *currMove, *lastMove, *firstMove;
  51. static int count;
  52. static SkewbLoc startLoc[MAXFACES][MAXCUBES];
  53.  
  54. static void InitStack();
  55. static void PushStack();
  56. static void PopStack();
  57. static int EmptyStack();
  58. static void FlushStack();
  59.  
  60. static void InitStack()
  61. {
  62.   if (!(lastMove = (MoveStack *) malloc(sizeof(MoveStack))))
  63.     XtError("Not enough memory, exiting.");
  64.   if (!(firstMove = (MoveStack *) malloc(sizeof(MoveStack))))
  65.     XtError("Not enough memory, exiting.");
  66.   firstMove->previous = lastMove->next = NULL;
  67.   firstMove->next = lastMove;
  68.   lastMove->previous = firstMove;
  69.   count = 0;
  70. }
  71.  
  72. static void PushStack(move)
  73.   MoveRecord move;
  74. {
  75.   if (!(currMove = (MoveStack *) malloc(sizeof(MoveStack))))
  76.     XtError("Not enough memory, exiting.");
  77.   lastMove->previous->next = currMove;
  78.   currMove->previous = lastMove->previous;
  79.   currMove->next = lastMove;
  80.   lastMove->previous = currMove;
  81.   currMove->move = move;
  82.   count++;
  83. }
  84.  
  85. static void PopStack(move)
  86.   MoveRecord *move;
  87. {
  88.   *move = lastMove->previous->move;
  89.   currMove = lastMove->previous;
  90.   lastMove->previous->previous->next = lastMove;
  91.   lastMove->previous = lastMove->previous->previous;
  92.   (void) free((void *) currMove);
  93.   count--;
  94. }
  95.  
  96. static int EmptyStack()
  97. {
  98.   return (lastMove->previous == firstMove);
  99. }
  100.  
  101. static void FlushStack()
  102. {
  103.   while (lastMove->previous != firstMove) {
  104.     currMove = lastMove->previous;
  105.     lastMove->previous->previous->next = lastMove;
  106.     lastMove->previous = lastMove->previous->previous;
  107.     (void) free((void *) currMove);
  108.   }
  109.   count = 0;
  110. }
  111.  
  112. /**********************************/
  113.  
  114. void InitMoves()
  115. {
  116.   InitStack();
  117. }
  118.  
  119. static void WriteMove(move, face, position, direction, control)
  120.   MoveRecord *move;
  121.   int face, position, direction, control;
  122. {
  123.   /* move.face = face; move.position = position;
  124.   move.direction = direction; move.control = control; */
  125.   move->packed = ((control & 0xF) << 12) + ((direction & 0xF) << 8) +
  126.     ((position & 0xF) << 4) + (face & 0xF);
  127. }
  128.  
  129. static void ReadMove(face, position, direction, control, move)
  130.   int *face, *position, *direction, *control;
  131.   MoveRecord move;
  132. {
  133.   /* *face = move->face; *position = move->position;
  134.   *direction = move.direction; *control = move.control; */
  135.   *face = move.packed & 0xF;
  136.   *position = (move.packed >> 4) & 0xF;
  137.   *direction = (move.packed >> 8) & 0xF;
  138.   *control = (move.packed >> 12) & 0xF;
  139. }
  140.  
  141. void PutMove(face, position, direction, control)
  142.   int face, position, direction, control;
  143. {
  144.   MoveRecord move;
  145.  
  146.   WriteMove(&move, face, position, direction, control);
  147.   PushStack(move);
  148. }
  149.  
  150. void GetMove(face, position, direction, control)
  151.   int *face, *position, *direction, *control;
  152. {
  153.   MoveRecord move;
  154.  
  155.   PopStack(&move);
  156.   ReadMove(face, position, direction, control, move);
  157. }
  158.  
  159. int MadeMoves()
  160. {
  161.   return !EmptyStack();
  162. }
  163.  
  164. void FlushMoves(w)
  165.   SkewbWidget w;
  166. {
  167.   int face, position;
  168.  
  169.   FlushStack();
  170.   for (face = 0; face < MAXFACES; face++)
  171.     for (position = 0; position < MAXCUBES; position++) {
  172.       startLoc[face][position].face = w->skewb.cubeLoc[face][position].face;
  173.       startLoc[face][position].rotation =
  174.         w->skewb.cubeLoc[face][position].rotation;
  175.     }
  176. }
  177.  
  178. int NumMoves()
  179. {
  180.   return count;
  181. }
  182.  
  183. void ScanMoves(fp, w, moves)
  184.   FILE *fp;
  185.   SkewbWidget w;
  186.   int moves;
  187. {
  188.   int face, position, direction, control, k;
  189.   char c;
  190.  
  191.   for (k = 0; k < moves; k++) {
  192.     while ((c = getc(fp)) != EOF && c != SYMBOL);
  193.     (void) fscanf(fp, "%d %d %d %d", &face, &position, &direction, &control);
  194.     MoveSkewb(w, face, position, direction, control);
  195.   }
  196. }
  197.  
  198. void PrintMoves(fp)
  199.   FILE *fp;
  200. {
  201.   int face, position, direction, control, counter = 0;
  202.  
  203.   currMove = firstMove->next;
  204.   (void) fprintf(fp, "moves\tface\tpos\tdir\tcon\n"); 
  205.   while (currMove != lastMove) {
  206.     ReadMove(&face, &position, &direction, &control, currMove->move);
  207.     (void) fprintf(fp, "%d%c\t%d\t%d\t%d\t%d\n",
  208.       ++counter, SYMBOL, face, position, direction, control); 
  209.     currMove = currMove->next;
  210.   }
  211. }
  212.  
  213. void ScanStartPosition(fp, w)       
  214.   FILE *fp;
  215.   SkewbWidget w;
  216. {
  217.   int face, position, num;
  218.   char c;
  219.  
  220.   while ((c = getc(fp)) != EOF && c != SYMBOL);
  221.   for (face = 0; face < MAXFACES; face++)
  222.     for (position = 0; position < MAXCUBES; position++) {
  223.       (void) fscanf(fp, "%d ", &num);
  224.       startLoc[face][position].face = num;
  225.       if (w->skewb.orient) {
  226.         (void) fscanf(fp, "%d ", &num);
  227.         startLoc[face][position].rotation = num;
  228.       }
  229.     }
  230. }
  231.  
  232. void PrintStartPosition(fp, w)       
  233.   FILE *fp;
  234.   SkewbWidget w;
  235. {
  236.   int face, position;
  237.  
  238.   (void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
  239.   for (face = 0; face < MAXFACES; face++) {
  240.     for (position = 0; position < MAXCUBES; position++) {
  241.       (void) fprintf(fp, "%d ", startLoc[face][position].face);
  242.       if (w->skewb.orient)
  243.         (void) fprintf(fp, "%d  ", startLoc[face][position].rotation);
  244.     }
  245.     (void) fprintf(fp, "\n");
  246.   }
  247. }
  248.  
  249. void SetStartPosition(w)       
  250.   SkewbWidget w;
  251. {
  252.   int face, position;
  253.  
  254.   for (face = 0; face < MAXFACES; face++)
  255.     for (position = 0; position < MAXCUBES; position++) {
  256.       w->skewb.cubeLoc[face][position].face = startLoc[face][position].face;
  257.       if (w->skewb.orient)
  258.          w->skewb.cubeLoc[face][position].rotation =
  259.            startLoc[face][position].rotation;
  260.     }
  261.   DrawAllPolyhedrons(w);
  262. }
  263.